package com.shop.controller;

import com.shop.annotation.RequiresPermissionsDesc;
import com.shop.pojo.DtsCategory;
import com.shop.service.DtsCategoryService;
import com.shop.service.DtsGoodsService;
import com.shop.util.ResponseUtil;
import com.shop.vo.CategoryAssist;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AdminCategoryController
 * @Author xiao宋
 * @Date 8:27  2022/10/17
 * @Version 1.0
 * @Description TODO
 */
@Slf4j
@RestController
@RequestMapping("/admin/category")
public class AdminCategoryController {
    @Autowired
    DtsGoodsService goodsService;
    @Autowired
    DtsCategoryService categoryService;

    @GetMapping("/l1")
    public Object L1list() {
        List<CategoryAssist> data = categoryService.findL1List();
        return ResponseUtil.ok(data);
    }

    /**
     * 商品类目查询
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param id    商品类目 ID
     * @param name  商品类目名
     * @return
     */
    @RequiresPermissions("admin:goods:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "商品管理"}, button = "类目查询")
    @GetMapping("/list")
    public Object footprint(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
            @RequestParam(value = "sort", required = false, defaultValue = "add_time") String sort,
            @RequestParam(value = "order", required = false, defaultValue = "desc") String order,
            Integer id, String name
    ) {

        log.debug("查询商品类目列表分页方法");
        log.debug("前端传过来的每页条数：{}，第几页：{}", page, limit);
        Map<String, Object> data = categoryService.findByPage(page, limit, sort, order, id, name);
        log.debug("查询商品类目列表分页方法显示完毕");

        return ResponseUtil.ok(data);
    }

    @PostMapping("/create")
    public Object create(@RequestBody DtsCategory category) {
        log.debug("新增类目方法开始执行");
        log.debug("接收到的商品等级：{}",category.getLevel());
        boolean flag = categoryService.creatCategory(category);
        if (flag){
            category.setUpdateTime(new Date());
            return ResponseUtil.ok(category);
        }else {
            return ResponseUtil.fail(-1,"商品类目添加失败");
        }
    }
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCategory category){

        categoryService.delete(category);
        return ResponseUtil.ok();
    }

}
